package com.eascs.banknet.boc.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.banknet.boc.vo.boc003.BOC003Element;
import com.eascs.banknet.common.dao.BaseO2oDao;
import com.eascs.banknet.common.entity.BankLoanData;
import com.eascs.banknet.common.entity.LoanUserEntity;
import com.eascs.banknet.common.entity.LoanapplyEntity;
import com.eascs.common.util.StringUtil;

/**
 * 003查询放款信息
 * @author user
 *
 */
@Repository
public class BOC003Dao extends BaseO2oDao {
	@Autowired
	private LoanapplyDao loanapplyDao;
	
	public List<BOC003Element> queryData(String startId){
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT t2.bank_applyno 'appno' ")
			.append(" FROM o2o_loanapply t1, o2o_loan_user t2 ")
			.append(" WHERE t1.loanapply_no = t2.loanapply_no")
			.append(" AND t2.bank_status in ('0109', '0149', '01L2') and (t1.data_source is null or t1.data_source!='2') ")
			.append(" AND t1.bank_type = 'BC' and t1.islock = 'N'")
			.append(" AND t1.loan_flg !='Y' and t2.bank_applyno IS NOT NULL ")
			.append(" ORDER BY t2.bank_applyno LIMIT 1");
		return super.findEntitys(sql.toString(), BOC003Element.class);
	}
	
	@Transactional("o2oTransactionManager")
	public void updateInfo(String appno, String loanapplyNo,String loan_amt,BigDecimal rate,String loanFlg, 
			String lafterm,String actno, Date fadate,Date endBankDate) throws Exception{
		LoanapplyEntity loanapply = super.getById(LoanapplyEntity.class, loanapplyNo);
		loanapply.setBankAmount(new BigDecimal(loan_amt));
		loanapply.setLoanrate(rate);
		loanapply.setLoanFlg(loanFlg);
		loanapply.setLoanTerm(Integer.valueOf(lafterm));
		loanapply.setBankActno(actno);
		loanapply.setPaymentTime(fadate);
		loanapply.setEndBankDate(endBankDate);
		//loanapply.setBankLoanName("BC");
		loanapply.setWfType("O");
		String billCode = loanapplyDao.getBillCodeByAppno(loanapplyNo);
		int rw1 = super.updateById(loanapply);
		if(rw1 <= 0){
			throw new RuntimeException("更新放款信息到贷款表失败，loanapplyNo:"+loanapplyNo);
		}
		LoanUserEntity loanUser = loanapplyDao.getLoanUserByAppno(appno);
		BankLoanData loanData = new BankLoanData();
		loanData.setBillCode(loanapply.getBillCode());
		loanData.setApplyAmount(loanapply.getLoanaMount());
		loanData.setLoanBank("BC");
		loanData.setLoanAmount(new BigDecimal(loan_amt));
		loanData.setLoanRate(rate);
		loanData.setLoanDate(fadate);
		loanData.setLoanTerm(Integer.valueOf(lafterm));
		loanData.setLoanerName(loanUser.getCustomerName());
		loanData.setLoanerCard(loanUser.getDocumentNo());
		loanData.setUpdateTime(new Date());
		loanData.setUpdatePsn("system");
		loanData.setIsDel("N");
		loanData.setDataType("3");
		String loanDataId = loanapplyDao.isBillCodeExist(billCode);
		if(StringUtil.hasText(loanDataId)){
			loanData.setBankLoanId(loanDataId);
			int rw2 = loanapplyDao.updateById(loanData);
			if(rw2 <= 0){
				throw new RuntimeException("修改银行放款信息失败，loanapplyNo:"+loanapplyNo);
			}
		}else{
			int rw2 = loanapplyDao.insert(loanData);
			if(rw2 <= 0){
				throw new RuntimeException("新增银行放款信息失败，loanapplyNo:"+loanapplyNo);
			}
		}
	}
}
